package com.huawei.hms.update.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.activity.IBridgeActivityDelegate;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.NetWorkUtil;
import com.huawei.hms.utils.PackageManagerHelper;
import com.huawei.hms.utils.Util;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public abstract class a implements IBridgeActivityDelegate {
    public WeakReference<Activity> a;
    public IBridgeActivityDelegate b;
    protected UpdateBean c = null;
    protected b d = null;
    protected boolean e = false;
    protected int f = -1;
    protected String g = null;
    protected String h = null;
    protected int i = 0;
    protected String j = null;

    abstract void a();

    abstract void a(Class<? extends b> cls);

    public static String a(int i) {
        switch (i) {
            case 0:
                return k.class.getName();
            case 2:
                return f.class.getName();
            case 4:
                return g.class.getName();
            case 5:
                return h.class.getName();
            case 6:
                return l.class.getName();
            default:
                return "";
        }
    }

    public void a(b bVar) {
    }

    public void b(b bVar) {
    }

    protected Activity b() {
        if (this.a == null) {
            return null;
        }
        return (Activity) this.a.get();
    }

    public void onBridgeActivityCreate(Activity activity) {
        this.a = new WeakReference(activity);
        if (this.c == null) {
            Intent intent = activity.getIntent();
            if (intent != null) {
                this.c = (UpdateBean) intent.getSerializableExtra(BridgeActivity.EXTRA_DELEGATE_UPDATE_INFO);
                if (this.c == null) {
                    return;
                }
            }
            return;
        }
        this.g = this.c.b();
        this.h = this.c.getClientAppName();
        this.i = this.c.c();
        this.j = this.c.d();
        this.b = null;
        this.e = false;
        this.f = -1;
    }

    public void onBridgeActivityDestroy() {
        this.a = null;
        c();
        if (this.e && this.b != null) {
            this.b.onBridgeActivityDestroy();
        }
    }

    public void onBridgeConfigurationChanged() {
        if (this.e && this.b != null) {
            this.b.onBridgeConfigurationChanged();
        } else if (this.d != null) {
            Class cls = this.d.getClass();
            this.d.c();
            this.d = null;
            a(cls);
        }
    }

    protected void c() {
        if (this.d != null) {
            try {
                this.d.c();
                this.d = null;
            } catch (IllegalStateException e) {
                HMSLog.e("AbsUpdateWizard", "In dismissDialog, Failed to dismiss the dialog." + e.getMessage());
            }
        }
    }

    protected void a(int i, int i2) {
        if (!HiAnalyticsUtil.getInstance().hasError()) {
            Context b = b();
            if (b != null && !b.isFinishing()) {
                int packageVersionCode = new PackageManagerHelper(b).getPackageVersionCode(this.g);
                Map hashMap = new HashMap();
                hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, b.getPackageName());
                hashMap.put(HiAnalyticsConstant.BI_KEY_TARGET_PACKAGE, this.g);
                hashMap.put(HiAnalyticsConstant.BI_KEY_TARGET_VER, String.valueOf(packageVersionCode));
                hashMap.put(HiAnalyticsConstant.BI_KEY_SDK_VER, String.valueOf(30003300));
                hashMap.put(HiAnalyticsConstant.BI_KEY_APP_ID, Util.getAppId(b));
                hashMap.put(HiAnalyticsConstant.BI_KEY_TRIGGER_API, "core.connnect");
                hashMap.put(HiAnalyticsConstant.BI_KEY_UPDATE_TYPE, String.valueOf(i2));
                hashMap.put(HiAnalyticsConstant.BI_KEY_NET_TYPE, String.valueOf(NetWorkUtil.getNetworkType(b)));
                hashMap.put(HiAnalyticsConstant.BI_KEY_RESUST, c(i, i2));
                HiAnalyticsUtil.getInstance().onEvent(b, HiAnalyticsConstant.BI_TYPE_UPDATE, hashMap);
            }
        }
    }

    private String c(int i, int i2) {
        String valueOf = String.valueOf(i);
        switch (i2) {
            case 0:
                return "0000" + valueOf;
            case 2:
                return "2000" + valueOf;
            case 4:
                return "6000" + valueOf;
            case 5:
                return "5000" + valueOf;
            case 6:
                return "4000" + valueOf;
            default:
                return valueOf;
        }
    }

    protected boolean a(boolean z) {
        Activity b = b();
        if (b == null) {
            return false;
        }
        ArrayList typeList = this.c.getTypeList();
        if (typeList.size() > 0) {
            typeList.remove(0);
        }
        if (this.b == null) {
            a(typeList);
        }
        if (this.b == null) {
            return false;
        }
        this.e = true;
        this.c.setTypeList(typeList);
        this.c.setNeedConfirm(z);
        this.b.onBridgeActivityCreate(b);
        return true;
    }

    private void a(ArrayList arrayList) {
        Exception e;
        String str = null;
        if (arrayList != null && arrayList.size() > 0) {
            str = a(((Integer) arrayList.get(0)).intValue());
        }
        if (str != null) {
            try {
                this.b = (IBridgeActivityDelegate) Class.forName(str).asSubclass(IBridgeActivityDelegate.class).newInstance();
            } catch (ClassCastException e2) {
                e = e2;
                HMSLog.e("AbsUpdateWizard", "getBridgeActivityDelegate error" + e.getMessage());
            } catch (InstantiationException e3) {
                e = e3;
                HMSLog.e("AbsUpdateWizard", "getBridgeActivityDelegate error" + e.getMessage());
            } catch (IllegalAccessException e4) {
                e = e4;
                HMSLog.e("AbsUpdateWizard", "getBridgeActivityDelegate error" + e.getMessage());
            } catch (ClassNotFoundException e5) {
                e = e5;
                HMSLog.e("AbsUpdateWizard", "getBridgeActivityDelegate error" + e.getMessage());
            }
        }
    }

    protected void b(int i, int i2) {
        Activity b = b();
        if (b != null && !b.isFinishing()) {
            a(i, i2);
            Intent intent = new Intent();
            intent.putExtra(BridgeActivity.EXTRA_DELEGATE_CLASS_NAME, getClass().getName());
            intent.putExtra(BridgeActivity.EXTRA_RESULT, i);
            b.setResult(-1, intent);
            b.finish();
        }
    }

    public void onKeyUp(int i, KeyEvent keyEvent) {
        if (this.e && this.b != null) {
            this.b.onKeyUp(i, keyEvent);
        }
    }

    protected boolean a(String str, int i) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Context b = b();
        if (b == null || b.isFinishing() || new PackageManagerHelper(b).getPackageVersionCode(str) < i) {
            return false;
        }
        return true;
    }
}
